package com.wz.leetcode.backtrack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LetterCombinations_17 {
    public static List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits.length() == 0) {
            return result;
        }
        Map<Character, String> phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backtrack(result, phoneMap, digits, 0, new StringBuilder());
        return result;
    }

    private static void backtrack(List<String> result,
                                  Map<Character, String> phoneMap,
                                  String digits,
                                  int index,
                                  StringBuilder builder) {
        if (index == digits.length()) {
            result.add(builder.toString());
        } else {
            char digit = digits.charAt(index);
            String lettes = phoneMap.get(digit);
            int lettersCount = lettes.length();
            for (int i = 0; i < lettersCount; i++) {
                builder.append(lettes.charAt(i));
                backtrack(result, phoneMap, digits, index + 1, builder);
                builder.deleteCharAt(index);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(letterCombinations("23"));
        System.out.println(letterCombinations(""));
        System.out.println(letterCombinations("2"));
        System.out.println(letterCombinations("3456"));
    }
}
